﻿@model Contrib.Taxonomies.ViewModels.TaxonomyFieldViewModel
@using Contrib.Taxonomies.Helpers;
@using Orchard.Utility.Extensions;

@{
    Style.Include("admin-taxonomy.css");
    Script.Include("~/Themes/TheAdmin/scripts/admin.js").AtFoot();
    Script.Include("admin-taxonomy-expando.js").AtFoot();

    if (Model.Settings.Autocomplete) {
        Style.Require("jQueryUI_Orchard");
        Script.Require("jQueryUI_Sortable");
        Script.Require("jQueryUI_Autocomplete");
        Script.Include("tagit.js").AtFoot();
        Style.Include("admin-taxonomy-tags.css");
        Script.Include("admin-taxonomy-tags.js").AtFoot();
    }

    int termIndex = 0, ti;
}
@functions {

    bool IsTermDisabled(TermEntry term) {
        return !term.Selectable || (Model.Settings.LeavesOnly && Model.Terms.Any(t => t.Path.Contains(term.Path + term.Id)));
    }

}
@{
    var allTerms = string.Join(",", Model.Terms.Select(x => string.Format("{0}\t{1}\t{2}\t{3}", x.Name.ToLower(), x.Id, x.GetLevels(), IsTermDisabled(x))));
    var selectedTerms = string.Join(",", Model.Terms.Where(x => x.IsChecked).Select(x => string.Format("{0}\t{1}", x.Name.ToLower(), x.Id)));
}

<fieldset class="taxonomy-wrapper" data-name-prefix="@Html.FieldNameFor(m => m)" data-id-prefix="@Html.FieldIdFor(m => m)">
    <legend>@Model.Name.CamelFriendly()</legend>
    @if (Model.Settings.Autocomplete) {
        <div class="terms-editor" data-all-terms="@allTerms" data-selected-terms="@selectedTerms" data-allow-new-terms="@Model.Settings.AllowCustomTerms.ToString().ToLower()" data-singlechoice="@Model.Settings.SingleChoice.ToString().ToLower()">
            <ul>
            </ul>
            @if (Model.Settings.SingleChoice) {
                <div class="hint">Enter a single term. Hit <i>tab</i> or <i>enter</i> to apply the term @if(!Model.Settings.AllowCustomTerms) { <text>. This taxonomy does not allow you to create new terms. </text> }</div>
            }
            else {
                <div class="hint">Enter multiple terms. Hit <i>tab</i>, <i>enter</i> or <i>,</i> to add multiple terms @if(!Model.Settings.AllowCustomTerms) { <text>. This taxonomy does not allow you to create new terms. </text> }</div>
            }
        </div>
    }
    
    @if (!String.IsNullOrWhiteSpace(Model.Settings.Hint))
    {
        <span class="hint">@Model.Settings.Hint</span>
    }

    <div class="hidden-taxonomy-state">
        <ul class="terms">
            @foreach (var entry in Model.Terms)
            {
                ti = termIndex;
                <li>
                    @{
                        var disabled = IsTermDisabled(entry);
                        if (Model.Settings.SingleChoice)
                        {
                            <input @if (disabled){ <text>disabled="disabled"</text> } type="radio" value="@Model.Terms[ti].Id" @if (entry.Id == Model.SingleTermId){ <text>checked="checked"</text> } name="@Html.FieldNameFor(m => m.SingleTermId)" id="@Html.FieldIdFor(m => m.Terms[ti].IsChecked)" data-term="@entry.Name.ToLower()" />
                        }
                        else
                        {            
                            <input @if (disabled){ <text>disabled="disabled"</text> } type="checkbox" value="true" @if (entry.IsChecked) { <text>checked="checked"</text> } name="@Html.FieldNameFor(m => m.Terms[ti].IsChecked)" id="@Html.FieldIdFor(m => m.Terms[ti].IsChecked)" data-term="@entry.Name.ToLower()" /> 
                        }
                    }
                    <label class="forcheckbox" for="@Html.FieldIdFor(m => m.Terms[ti].IsChecked)">@entry.Name.ToLower()</label>
                    @Html.HiddenFor(m => m.Terms[ti].Id)
                </li>
                termIndex++;
            }
        </ul>
    </div>
            
    @if (!Model.Terms.Any())
    {
        <div class="no-terms">
            There are no terms defined for @Model.Name.CamelFriendly() yet.
            <a href="@Url.Action("Index", "TermAdmin", new { taxonomyId = Model.TaxonomyId, area = "Contrib.Taxonomies" })">Create some terms</a>
        </div>        
    }
    @Html.HiddenFor(m => m.TaxonomyId)
</fieldset>
